package br.com.massai.model.business;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.com.massai.model.dao.ProductionGroupRepository;
import br.com.massai.model.entity.ProductionGroup;
import br.com.massai.model.exceptions.ErrorCore;
import br.com.massai.model.exceptions.SystemException;

@Stateless
public class ProductionGroupFacade implements ProductionGroupBusiness {

	private static final long serialVersionUID = -8324538155608445944L;
	
	@EJB private ProductionGroupRepository productionGroupRepository;

	@Override
	public ProductionGroup findID(Long id) throws SystemException {
		return productionGroupRepository.findID(id);
	}

	@Override
	public ProductionGroup save(ProductionGroup entity) throws SystemException {
		checkBusinessRule(entity);
		if(entity.getId() == null) return productionGroupRepository.create(entity);
		else return productionGroupRepository.update(entity);
	}

	@Override
	public void delete(ProductionGroup entity) throws SystemException {
		productionGroupRepository.delete(entity);
	}

	@Override
	public void checkBusinessRule(ProductionGroup entity) throws SystemException {
		if(entity == null) throw new SystemException(ErrorCore.ERROR_ILLEGAL_ARGUMENT);
		if(entity.getNameGroup() == null || entity.getNameGroup().isEmpty()) throw new SystemException(ErrorCore.ERROR_REQUIRED_FIELD, "Nome");
	}

	@Override
	public List<ProductionGroup> autocompleteMethod(String str) throws SystemException {
		return productionGroupRepository.autocompleteMethod(str);
	}
}